package cn.cai.pethome.emp.controller;


import cn.cai.pethome.base.util.AjaxResult;
import cn.cai.pethome.base.util.PageList;
import cn.cai.pethome.emp.domain.Employee;
import cn.cai.pethome.emp.query.EmployeeQuery;
import cn.cai.pethome.emp.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    @GetMapping
    @ApiOperation(value = "查询员工")
    public List<Employee> queryEmployee(){
        return employeeService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "高级分页查询员工")
    public PageList<Employee> queryEmployee(@RequestBody EmployeeQuery query){
       return employeeService.queryAll(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除员工" )
    public AjaxResult employee(@PathVariable("id") Long id){
        try {
            employeeService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除数据" )
    public AjaxResult emp(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或者修改员工 如果id为null则添加否则修改" )
    public AjaxResult employee(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
